<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

//引入hash

class AdminRepository
{

    /**
     * 根据ID或者管理员姓名获取管理员记录详情
     *
     * @param $id 要查询的参数值
     * @param  string  $key  要查询的字段key值
     * @param  bool|false  $role_detail  是否查询管理员角色详情
     * @param  bool|false  $strict  是否按照严格模式查询,是则只查询正常启用的超管
     * @return mixed
     * @throws BasicException
     */
    public function admin_detail($id, $key = 'admin_id', $role_detail = false, $strict = false)
    {
        if (!in_array($key, ['admin_id', 'admin_name', 'phone_number'])) {
            throw new BasicException(90001);
        }
        //查询详情
        $admin = app('model_admin')->where("$key", $id);

        if ($role_detail) {
            if ($strict) {
                $admin = $admin->with([
                    'admin_role' => function ($query) {
                        $query->where('status', 1);
                    },
                ]);
            } else {
                $admin = $admin->with('admin_role');
            }
        }

        $admin = $admin->first();

        return $admin;
    }


    /**
     * 添加管理员
     *
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function adminAdd($param)
    {
        $admin = app('model_admin')->create($param);
        if ($admin) {
            return $admin;
        }
        throw new BasicException(90006);
    }


    /**
     * 获取管理员列表
     *
     * @param $sc_id
     * @param $param
     * @param  int  $page
     * @param  int  $perPage
     * @return mixed
     */
    public function adminList($param, $page = 1, $perPage = 10)
    {
        $list = app('model_admin');
        $whereFunc = function ($query) use ($param) {
            if (!empty($param["keyword"])) {
                $query->where('email', 'like', "%" . $param['keyword'] . "%")
                    ->orWhere('admin_name', 'like', "%" . $param['keyword'] . "%")
                    ->orWhere('phone_number', 'like', "%" . $param['keyword'] . "%");
            }
        };

        $list = $list->select('admin_id', 'phone_number', 'admin_name', 'email', 'realname', 'role_id', 'addtime', 'last_login_time', 'last_login_ip', 'login_count', 'status')
            ->with('admin_role')->orderBy('addtime', 'desc')->where($whereFunc)->paginate($perPage, ['*'], '', $page);

        foreach ($list as &$val) {
            $val->addtime = date('Y-m-d H:i:s', $val->addtime);
            $val->last_login_time = date('Y-m-d H:i:s', $val->last_login_time);
            if ($val->role_id == 0) {
                $val->role = '超级管理员';
            } else {
                $val->role = $val->admin_role->role_name ?? '';
            }

            $val->realname = empty($val->realname) ? "" : $val->realname;
            unset($val->admin_role);
        }

        return $list;
    }


    /**
     * 根据sc_id获取第一条注册的用户信息
     *
     * @param $sc_id
     * @return mixed
     */
    public function getAdminDetailBy($sc_id)
    {
        $admin = app('model_admin')->where('sc_id', $sc_id)->orderBy('add_time', 'desc')->first();

        return $admin;
    }


    /**
     * 获取厅的管理员详情
     *
     * @param $sc_id
     * @return mixed
     */
    public function getHallAdminDetail($sc_id)
    {
        $admin = app('model_admin')->where('sc_id', $sc_id)->with('admin_role')->first();

        //        dd($admin);
        return $admin;
    }

    /**
     * 获取所有管理员信息
     *
     * @return mixed
     */
    public function getAllAdmins()
    {
        $admin = app('model_admin')->select('admin_id')->get()->toArray();

        return $admin;
    }

    /**
     * 修改管理员密码
     *
     * @param $admin_id
     * @param $password
     * @return mixed
     */
    public function savePassword($admin_id, $password)
    {
        $admin = app('model_admin')->where('admin_id', $admin_id)->update(['new_password' => $password]);

        return $admin;
    }

    /**
     * 更新管理员状态
     *
     * @param $admin_id
     * @param $param
     * @return BasicException
     */
    public function editAdmin($id, $param)
    {
        $admin = app('model_admin')->where('admin_id', $id)->update($param);
        if ($admin !== false) {
            return true;
        }
        throw new BasicException(20008);
    }

    public function delAdmin($ids = [])
    {
        $delConfig = app('model_admin')->whereIn("admin_id", $ids)->delete();

        if ($delConfig) {
            return $delConfig;
        }

        if ($delConfig) {
            return $delConfig;
        }
        throw new BasicException(93044);
    }


    /**
     * 更新管理员状态
     *
     * @param $admin_id
     * @param $status
     * @return mixed
     */
    public function updateAdminStatus($admin_id, $status)
    {
        $admin = app('model_admin')->where('admin_id', $admin_id)->update(['status' => $status]);
        if ($admin) {
            return $admin;
        }
        throw new BasicException(20007);
    }

    /**
     * 忘记密码
     *
     * @param $admin_id
     * @param $password
     * @return mixed
     */
    public function forgetPassword($admin_id, $password)
    {
        $admin = app('model_admin')->where('admin_id', $admin_id)->update(['new_password' => $password]);
        if ($admin) {
            return $admin;
        }
        throw new BasicException(20009);
    }

    /**
     * 根据admin_id获取管理员详情
     *
     * @param $admin_id
     * @return mixed
     */
    public function getAdminDetail($admin_id)
    {
        $admin = app('model_admin')->where('admin_id', $admin_id)
            ->select('admin_id', 'admin_name', 'email', 'disabled', 'is_admin', 'realname', 'role_id', 'avatar', 'status')->first();
        if ($admin) {
            return $admin;
        }
        throw new BasicException(20010);
    }

    function adminLog($log_info = '', $log_sql = '')
    {
        $request = app("request");
        $action = $request->route()[1]['uses'];

        [$class, $method] = explode('@', $action);
        $controller = substr(strrchr($class, '\\'), 1);

        $data['admin_id'] = $request->admin_id;
        $data['client_ip'] = $request->getClientIp();
        $data['dateline'] = time();
        $data['uri'] = $request->path();
        $data['action_name'] = $controller;
        $data['method_name'] = $method;
        $data['content'] = serialize($log_info);

        app("model_admin_log")->create($data);
    }
}
